//
//  SWCategoryCollectionCell.swift
//  ChildStory
//
//  Created by linhan on 2016/11/24.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
class SWCategoryCollectionCell: UICollectionViewCell
{
    weak var event:SWKidEvent? {
        didSet {
            if _change
            {
                layoutSubviews()
            }
        }
    }
    
    //边框
    private let BorderWidth:CGFloat = 2
    
    private var _change:Bool = true
    
    private var _inited:Bool = false
    
    lazy private var _addIcon:UILabel = UILabel()
    lazy private var _label:UILabel = UILabel()
    lazy private var _imageView:ImageLoader = ImageLoader()
    lazy private var _contentView:UIView = UIView()
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        setup()
    }
    
    deinit
    {
        //trace("DEINIT SWAssetCollectionCell")
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        
        
        _contentView.frame = CGRect(x: BorderWidth, y: BorderWidth, width: width - 2 * BorderWidth, height: height - 2 * BorderWidth)
        _contentView.backgroundColor = UIColor.white
        contentView.addSubview(_contentView)
        
        
        _imageView.frame = _contentView.bounds
        _imageView.contentMode = .scaleAspectFill
        //_imageView.clipsToBounds = true
        _contentView.addSubview(_imageView)
        
        let maskView:UIView = UIView()
        maskView.frame = _contentView.bounds
        maskView.backgroundColor = UIColor(white: 0, alpha: 0.2)
        _contentView.addSubview(maskView)
        
        _label.frame = CGRectMake(0, 54, bounds.width, 18)
        _label.font = UIFont.systemFont(ofSize: 14)
        _label.textColor = UIColor.white
        _label.textAlignment = .center
        _contentView.addSubview(_label)
        
        let viewAllImage = UIImageView(image: UIImage(named:"album_cell_img_viewall"))
        viewAllImage.center = CGPoint(x: _contentView.width * 0.5, y: _label.bottom + 19)
        _contentView.addSubview(viewAllImage)
        
        _addIcon.text = "按+添加照片"
        _addIcon.sizeToFit()
        _addIcon.center = CGPoint(x: _contentView.width * 0.5, y: viewAllImage.bottom + 19)
        _addIcon.isHidden = true
        _contentView.addSubview(_addIcon)
    }
    
    override var isSelected:Bool{
        get{
            return super.isSelected
        }
        set{
            super.isSelected = newValue
            //_addIcon.isHidden = !newValue
        }
    }
    
    override func prepareForReuse()
    {
        super.prepareForReuse()
        _change = true
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        if let event = event , _change
        {
            _change = false
            _label.text = event.title ?? ""
            _imageView.load(event.coverURL ?? "")
        }
    }
    
}
